package com.dkd.testUser.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.fastjson2.JSON;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.core.domain.R;
import com.dkd.common.core.page.TableDataInfo;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.testUser.domain.TestUser;
import com.dkd.testUser.service.ITestUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 测试用户表 前端控制器
 * </p>
 *
 * @author zwy
 * @since 2025-04-28
 */
@Api(tags = "用户商品管理")
@RestController
@RequestMapping("/testUser")
public class TestUserController extends BaseController {
    @Autowired
    private ITestUserService testUserService;

    /**
     * 导出
     */

    @ApiOperation("导出用户列表")
    @PreAuthorize("@ss.hasPermi('testUser:user:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        List<TestUser> list = testUserService.selectList();
        ExcelUtil<TestUser> util = new ExcelUtil<>(TestUser.class);
        util.exportEasyExcel(response,list, "用户数据");
    }

    /**
     * 导入
     */
    @ApiOperation("导入用户列表")
    @PreAuthorize("@ss.hasPermi('testUser:user:import')")
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<TestUser> util = new ExcelUtil<>(TestUser.class);
        List<TestUser> testUsers = util.importEasyExcel(file.getInputStream());
        return toAjax(testUserService.insertTestUsers(testUsers));
    }

    /**
     * 查询用户列表
     */
    @ApiOperation("查询用户列表")
    @PreAuthorize("@ss.hasPermi('testUser:user:list')")
    @GetMapping("/list")
    public TableDataInfo list() {
        startPage();
        return getDataTable(testUserService.selectList());
    }

    /**
     * 详情
     *
     * @param id
     * @return 写入了redis
     */
    @ApiOperation("查询用户详情")
    @PreAuthorize("@ss.hasPermi('testUser:user:query')")
    @GetMapping(value = "/{id}")
    public R<TestUser> getInfo(@PathVariable("id") Integer id) {
        return R.ok(testUserService.selectById(id));
    }

    /**
     * 新增用户以及商品
     */
    @ApiOperation("新增用户以及商品")
    @PreAuthorize("@ss.hasPermi('testUser:user:add')")
    @PostMapping
    public AjaxResult getInfo(@RequestBody TestUser testUser) {
        return AjaxResult.success(testUserService.insertTestUser(testUser));
    }

    /**
     * 删除用户
     */
    @ApiOperation("批量删除用户")
    @PreAuthorize("@ss.hasPermi('testUser:user:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(testUserService.deleteTestUserByIds(ids));
    }

    /**
     * 修改
     */
    @ApiOperation("修改用户")
    @PreAuthorize("@ss.hasPermi('testUser:user:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody TestUser testUser) {
        return toAjax(testUserService.updateTestUser(testUser));
    }
}
